﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using DotNet.Random;

namespace AutoAddClient
{
    public class AutoAddService
    {
        public static IList<Domain.Entity.MobileClient> RandCurrentDayDevice(int count)
        {
            var data = new List<Domain.Entity.MobileClient>();
            var token = new DeviceTokenRandService();
            var name = new DeviceNameRandService();
            var deviceRandService = new DeviceRandService(null, token, name);
            for (int i = 0; i < count; i++)
            {
                var temp = deviceRandService.Rand();
                data.Add(temp);
                System.Threading.Thread.Sleep(100);
            }
            return data;
        }

        public static IList<Domain.Entity.MobileClient> RandYesterdayDevice(int count)
        {
            var data = RandCurrentDayDevice(count);
            foreach (var item in data)
            {
                item.AddTime = DotNet.Random.RandService.RandDateTimeCurrentYearMonthYesterday();
                System.Threading.Thread.Sleep(500);
            }
            return data;
        }

        public static IList<Domain.Entity.MobileClient> RandMaxMinDevice(int count, System.DateTime min, System.DateTime max)
        {
            var data = RandCurrentDayDevice(count);
            foreach (var item in data)
            {
                item.AddTime = DotNet.Random.RandService.RandDateTime(min, max);
                System.Threading.Thread.Sleep(500);
            }
            return data;
        }

        public static IList<Domain.Entity.MobileClient> RandMaxMinExceptDevice(int count, System.DateTime min, System.DateTime max, IDictionary<int, int> except)
        {
            var data = RandCurrentDayDevice(count);
            foreach (var client in data)
            {
                var time = DotNet.Random.RandService.RandDateTime(min, max);
                var b = true;
                while (b)
                {
                    var tempCount = 0;
                    foreach (var item in except)
                    {
                        if (time.Hour.IsInBetweenAnd(item.Key, item.Value))
                        {
                            System.Threading.Thread.Sleep(200);
                            time = DotNet.Random.RandService.RandDateTime(min, max);
                            break;
                        }
                        else
                        {
                            tempCount++;
                        }
                    }
                    if (tempCount == except.Count)
                    {
                        b = false;
                    }
                }
                client.AddTime = time;
                System.Threading.Thread.Sleep(300);
            }
            return data;
        }

        public static void Add(IList<Domain.Entity.MobileClient> entity)
        {
            var clientRepository = GetClientRepository();
            if (entity != null && entity.Count > 0)
            {
                foreach (var item in entity)
                {
                    clientRepository.Add(item);
                }
                clientRepository.Uow.Commit();
            }
        }

        public static Infrastructure.Data.Uow.EfRepository<Domain.Entity.MobileClient> GetClientRepository()
        {
            var uow = new Infrastructure.Data.Uow.UnitOfWorkContext();
            var clientRepository = new Infrastructure.Data.Uow.EfRepository<Domain.Entity.MobileClient>(uow);
            return clientRepository;
        }
    }
}
